var onlineStats = null;// display the information of online/offline trackers
var onlineStats2 = null;// display the information of online/offline trackers

var isOnlineStatsShown = false;
var onlineStatsOrigPos = [];

var isOnlineStatsShown2 = false;
var onlineStatsOrigPos2= [];

var onlineUpdateInterval = 20000;

function getOnlineStats(){
	Ext.Ajax.request({
	    url: './getOnlineStats.jsp',
	    params: 'uin='+getOnboardTrackersStr(),
	    success:function(res){
			if(res.status == 404 ||
					res.responseText.indexOf('setLoginInfo') != -1){
				return;
			}
			var text = res.responseText;
			var s = text.split("-");
			document.getElementById("onlineTracker").innerHTML = s[0];
			document.getElementById("offTracker").innerHTML = s[1];
			window.setTimeout(getOnlineStats, onlineUpdateInterval);
		},
		failure: function(){
	    	window.setTimeout(getOnlineStats, onlineUpdateInterval);
	    }
	});
}

function createOnlineStats(pos){
   if(!onlineStats){
	   Ext.get('onlineStatsEl').setStyle('display','');
	   /*onlineStats = new Ext.Panel({
	        title: '&nbsp;',
	        renderTo:Ext.getBody(),
	        //html:'test',
	        contentEl: 'onlineStatsEl',
	        animCollapse:false,
	        cls:'empty',
	        height:50,
	        width:200,
	        draggable:true,
	        draggable:{
	            insertProxy:false,//拖动时不虚线显示原始位置
	            onDrag:function(e){
	                var pel = this.proxy.getEl();
	                this.x = pel.getLeft(true);
	                this.y = pel.getTop(true);//获取拖动时panel的坐标
	            },
	            endDrag:function(e){
	                //this.panel.setPosition(this.x, this.y);//移动到最终位置
	            }
	        },
	        tools: [{id:'minimize',handler:function(){closeOnlinestats();onlinestatsClosedAction = true;}},
	               {id:'restore',handler:function(){ showOnlinestats();}}],
	        style: {display:'',position:'absolute',zIndex:1000}
	        
	    }); realtimePanel */
	   //alert(document.getElementById('grid-trackers').getElementsByTagName('div')[0].id);
	   //alert(document.getElementById('grid-trackers').getElementsByTagName('div')[4].id);
	   
	   onlineStats = new Ext.Panel({
	        //title: '&nbsp;',
	        //renderTo:Ext.getCmp(document.getElementById('grid-trackers').getElementsByTagName('div')[4].id),
	        //renderTo:document.getElementById('grid-trackers').getElementsByTagName('div')[4].id,
	        renderTo:'td_logininfo_online',
	        //html:'test',
	        contentEl: 'onlineStatsEl',
	        animCollapse:false,
	        cls:'empty',
	        height:13,
	        width:130,
	        border:false,
	        //x:50,
	        //y:60,
	        //tools: [{id:'minimize',handler:function(){closeOnlinestats();onlinestatsClosedAction = true;}},{id:'restore',handler:function(){ showOnlinestats();}}],
	        style: {display:'',backgroundColor:'Transparent',zIndex:1000}
	        
	    });
	   onlineStats.on("expand",function(panel){
			isOnlineStatsShown = true;
	    });
		
	   	onlineStats.on("collapse",function(panel){
			isOnlineStatsShown = false;
	    });
	   	onlineStatsOrigPos = pos;
		setOnlineStatsOriginalPosition();
		getOnlineStats();
   }
}

function closeOnlinestats(duration){
	
	onlineStats.collapse(); 
	onlineStats.setTitle('');
	onlineStats.setWidth(30);
	onlineStats.getTool('restore').setVisible(true);
	onlineStats.getTool('minimize').setVisible(false);
	setOnlineStatsOriginalPosition();
	
}

function showOnlinestats(duration){

	if(typeof duration == 'undefined'){
		duration = 2;
    }
	onlineStats.setTitle('&nbsp;');
	onlineStats.setWidth(250);
	onlineStats.expand(); 
	
	onlineStats.getTool('restore').setVisible(false);
	onlineStats.getTool('minimize').setVisible(true);
	setOnlineStatsOriginalPosition(onlineStatsOrigPos);
	
	/*
	promt.setStyle('height','250px');
	promt.slideIn('b', { duration: duration, callback:function(){
		isAlarmShown = true;
	} });*/
	
}

function setOnlineStatsOriginalPosition(pos, adjWidth, adjHeight){
	if(!adjWidth){
		adjWidth = 0;
	} 
	if(!adjHeight){
		adjHeight = 0;
	} 
	if(!pos)
		pos = onlineStatsOrigPos;
	else
		onlineStatsOrigPos = pos;
	if(onlineStats)
		{}
		 //onlineStats.setPosition(pos[0] + adjWidth, pos[1] - 50 + adjHeight);
	     //onlineStats.setPosition(pos[0]-adjWidth-230, pos[1]-50-350);
	     //onlineStats.setPosition(30,1);
}
//-------------

function createOnlineStats2(pos){
   if(!onlineStats2){
	   Ext.get('bottomGpsInfo2').setStyle('display','');
	   onlineStats2 = new Ext.Panel({
	        title: '&nbsp;',
	        renderTo:Ext.getBody(),
	        //html:'test',
	        contentEl: 'bottomGpsInfo2',
	        animCollapse:false,
	        cls:'empty',
	        height:215,
	        width:166,
//	        draggable:true,
//	        draggable:{
//	            insertProxy:false,//拖动时不虚线显示原始位置
//	            onDrag:function(e){
//	                var pel = this.proxy.getEl();
//	                this.x = pel.getLeft(true);
//	                this.y = pel.getTop(true);//获取拖动时panel的坐标
//	            },
//	            endDrag:function(e){
//	                //this.panel.setPosition(this.x, this.y);//移动到最终位置
//	            }
//	        },
	        tools: [{id:'minimize',handler:function(){closeOnlinestats2();onlinestatsClosedAction2 = true;}},
	               {id:'restore',handler:function(){ showOnlinestats2();}}],
	        style: {display:'',position:'absolute',zIndex:1001}
	        
	    });
	   //alert(document.getElementById('grid-trackers').getElementsByTagName('div')[0].id);
	   //alert(document.getElementById('grid-trackers').getElementsByTagName('div')[4].id);
	   /*
	   onlineStats2 = new Ext.Panel({
	        //title: '&nbsp;',
	        //renderTo:Ext.getCmp(document.getElementById('grid-trackers').getElementsByTagName('div')[4].id),
	        //renderTo:document.getElementById('grid-trackers').getElementsByTagName('div')[4].id,
	        renderTo:'td_logininfo_online',
	        //html:'test',
	        contentEl: 'onlineStatsEl',
	        animCollapse:false,
	        cls:'empty',
	        height:13,
	        width:130,
	        border:false,
	        //x:50,
	        //y:60,
	        //tools: [{id:'minimize',handler:function(){closeOnlinestats2();onlinestatsClosedAction = true;}},{id:'restore',handler:function(){ showOnlinestats();}}],
	        style: {display:'',backgroundColor:'Transparent',zIndex:1000}
	    });*/
	   onlineStats2.el.setOpacity(0.7,true);//true 表示动画效果显示
	   onlineStats2.on("expand",function(panel){
			isOnlineStatsShown2 = true;
	    });
	   	onlineStats2.on("collapse",function(panel){
			isOnlineStatsShown2 = false;
	    });
	   	onlineStatsOrigPos2 = pos;
		setOnlineStatsOriginalPosition2();
		getOnlineStats2();
		
		closeOnlinestats2();
   }
}
function getOnlineStats2(){
/*
	Ext.Ajax.request({
	    url: './getOnlineStats.jsp',
	    params: 'uin='+getOnboardTrackersStr(),
	    success:function(res){
			if(res.status == 404 ||
					res.responseText.indexOf('setLoginInfo') != -1){
				return;
			}
			var text = res.responseText;
			var s = text.split("-");
			document.getElementById("onlineTracker").innerHTML = s[0];
			document.getElementById("offTracker").innerHTML = s[1];
			window.setTimeout(getOnlineStats, onlineUpdateInterval);
		},
		failure: function(){
	    	window.setTimeout(getOnlineStats, onlineUpdateInterval);
	    }
	});
	*/
}
function closeOnlinestats2(duration){
	
	onlineStats2.collapse(); 
	onlineStats2.setTitle('');
	onlineStats2.setWidth(30);
	onlineStats2.getTool('restore').setVisible(true);
	onlineStats2.getTool('minimize').setVisible(false);
	setOnlineStatsOriginalPosition2();
	//setOnlineStatsOriginalPosition2(null,0,);
}

function showOnlinestats2(duration){

	if(typeof duration == 'undefined'){
		duration = 2;
    }
	onlineStats2.setTitle('&nbsp;');
	onlineStats2.setWidth(166);
	onlineStats2.expand(); 
	
	onlineStats2.getTool('restore').setVisible(false);
	onlineStats2.getTool('minimize').setVisible(true);
	setOnlineStatsOriginalPosition2(onlineStatsOrigPos2);
	
	/*
	promt.setStyle('height','250px');
	promt.slideIn('b', { duration: duration, callback:function(){
		isAlarmShown = true;
	} });*/
	
}
function setOnlineStatsOriginalPosition2(pos, adjWidth, adjHeight){
	if(!adjWidth){
		adjWidth = 0;
	} 
	if(!adjHeight){
		adjHeight = 0;
	} 
	if(!pos)
		pos = onlineStatsOrigPos2;
	else
		onlineStatsOrigPos2 = pos;
	if(onlineStats2)
		{
		//alert(pos[0] + adjWidth);
		//alert(pos[1] - 50 + adjHeight);
		onlineStats2.setPosition(pos[0] + adjWidth,pos[1]-218-adjHeight);
		}
		 //onlineStats2.setPosition(pos[0] + adjWidth, pos[1] - 50 + adjHeight);
	     //onlineStats2.setPosition(pos[0]-adjWidth-230, pos[1]-50-350);
	     //onlineStats2.setPosition(30,1);
}